//
//  AddressPickerVIew.m
//  Yuncang
//
//  Created by ChengxuZheng on 16/1/9.
//  Copyright © 2016年 ChengxuZheng. All rights reserved.
//

#import "AddressPickerView.h"

#import "ZLAdministratorModel.h"

#define Hidden CGRectMake(0, kSCREENH_HEIGHT, kSCREEN_WIDTH, 250)
#define Show CGRectMake(0, kSCREENH_HEIGHT-250, kSCREEN_WIDTH, 250)

@implementation AddressPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor lightGrayColor];
        self.hidden = YES;
        self.alpha = .7f;
        
        [self getAddress];
        [self addChooseAddress];
        
        UITapGestureRecognizer *showGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showGRAction)];
        [self addGestureRecognizer:showGR];
    }
    return self;
}

#pragma mark - 
- (void)getAddress {
    
    
    [[YCRequestDataService sharedManager] Post:@"user/user/division_list" parameters:nil success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
            
        } else {
            _dataArr = [NSMutableArray new];
            
            NSArray *lists = response[@"response_data"][@"lists"];
            [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZLAdministratorModel *model = [[ZLAdministratorModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
                
                if (idx == 0) {
                    _model = model;
                    [[YCRequestDataService sharedManager] Post:@"user/user/division_list" parameters:@{@"parent_id":_model.x_id} success:^(id response) {
                        
                        if (!response[@"error_code"]) {
                            _subDataArr = [NSMutableArray new];
                            NSArray *lists = response[@"response_data"][@"lists"];
                            [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                ZLAdministratorModel *model = [[ZLAdministratorModel alloc] initWithDataDic:obj];
                                [_subDataArr addObject:model];
                                if (idx == 0) {
                                    _model = model;
                                }
                            }];
                         
                            [_pickerView reloadComponent:1];
                        }
                        
                    } failure:^{
                        NSLog(@"网络或服务器出错");
                    }];
                }
            }];
            [_pickerView reloadComponent:0];
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
}


#pragma mark - subviews
- (void)addChooseAddress {
    _chooseView = [[UIView alloc] initWithFrame:Hidden];
    _chooseView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_chooseView];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 220)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_chooseView addSubview:_pickerView];
    
    _cnlBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _cnlBtn.frame = CGRectMake(0, 5, 60, 30);
    [_cnlBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cnlBtn setTitleColor:kRGB_COLOR(237, 40, 46, 1) forState:UIControlStateNormal];
    [_cnlBtn addTarget:self action:@selector(chooseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:_cnlBtn];
    
    _esrBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _esrBtn.frame = CGRectMake(kSCREEN_WIDTH-60, 5, 60, 30);
    [_esrBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_esrBtn setTitleColor:kRGB_COLOR(237, 40, 46, 1) forState:UIControlStateNormal];
    [_esrBtn addTarget:self action:@selector(chooseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:_esrBtn];
}

#pragma mark - pickerview datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _dataArr.count;
    } else {
        return _subDataArr.count;
    }
}

#pragma mark - pickerview delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *myLabel = [[UILabel alloc] init];
    myLabel.textAlignment = NSTextAlignmentCenter;
    myLabel.font = kMyFont(14);
    myLabel.backgroundColor = [UIColor clearColor];
    
    if (component == 0) {
        ZLAdministratorModel *model = _dataArr[row];
        myLabel.text = model.name;
    } else {
        ZLAdministratorModel *model = _subDataArr[row];
        myLabel.text = model.name;
    }
    return myLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        _model = _dataArr[row];
        [[YCRequestDataService sharedManager] Post:@"user/user/division_list" parameters:@{@"parent_id":_model.x_id} success:^(id response) {
            
            if (!response[@"error_code"]) {
                _subDataArr = [NSMutableArray new];
                NSArray *lists = response[@"response_data"][@"lists"];
                [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    ZLAdministratorModel *model = [[ZLAdministratorModel alloc] initWithDataDic:obj];
                    [_subDataArr addObject:model];
                    if (idx == 0) {
                        _model = model;
                    }
                }];
                
                [_pickerView reloadComponent:1];
                
                [pickerView selectRow:0 inComponent:1 animated:YES];
            }
            
        } failure:^{
            NSLog(@"网络或服务器出错");
        }];
    } else {
        _model = _subDataArr[row];
    }
}

#pragma mark - gesture recognizer action
- (void)showGRAction {
    if (self.hidden) {
        self.hidden = !self.hidden;
        [UIView animateWithDuration:.35 animations:^{
            _chooseView.frame = Show;
        }];
    } else {
        self.hidden = !self.hidden;
        [UIView animateWithDuration:.35 animations:^{
            _chooseView.frame = Hidden;
        }];
    }
}

#pragma mark - button action
- (void)chooseBtnAct:(UIButton *)sender {
    if (sender == _cnlBtn) {
        [self showGRAction];
    } else {
        [self showGRAction];
        
        if (_enterCallBack) {
            _enterCallBack(_model);
        }
    }
}

#pragma mark -
- (void)showPicker {
    
    self.hidden = !self.hidden;
    
    [UIView animateWithDuration:.35 animations:^{
        _chooseView.frame = Show;
    }];
}



@end
